<?php

class Ship_plan_controller extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
        
        Validator::is_access($this->session->userdata('user_id'));
    }

    function index(){

        $section = "ship_plan";
        $cfer = new Cfer(array(
        _LB_SHIP_PLAN => base_url('ship_plan')));
        $title = _LB_SHIP_PLAN;

        $this->data['page_title'] = _LB_SHIP_PLAN;

        $filter = array();
        $filter['name'] = $this->input->post('name');
        $filter['start_date'] = $this->input->post('start_date');
        $filter['end_date'] = $this->input->post('end_date');
        $filter['region'] = $this->input->post('region');
        $filter['country'] = $this->input->post('country');
        $ship_plan = ShipPlan::getList($filter);

        $this->data['ship_plan'] = $ship_plan;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        $country = Country::getList(array('disabled'=>'0'));
        $region = Region::getList();
        $filter2 = array();
        $filter2['parent_id'] = 0;
        $filter2['type'] = 1;
        Menu::getMenuList($array_menus, $filter2);
        $this->data['arr_purposes'] = array(1=>_LB_GOODS_RECEIVER, 2=>_LB_GOODS_RETURN);
        
        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['region'] = $region;
        $this->data['cfer'] = $cfer;
        $this->load->view('ship_plan_list', $this->data);
    }

    function delete($id = null) {
        $ship_plan = new ShipPlan();

        if ($id && !$ship_plan->get($id)) {
                redirect(base_url('ship_plan'));
        }
        $ship_plan->delete();
        redirect(base_url('ship_plan'));
    }
    
    function form($id = null) {
        $section = "ship_plan";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_SHIP_PLAN => 'ship_plan',
            _LB_EDIT_SHIP_PLAN => base_url('ship_plan/form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_SHIP_PLAN => 'ship_plan',
            _LB_ADD_SHIP_PLAN => base_url('ship_plan/form/')));
        }
        
        $act = $this->input->get_post('act');
    
        $ship_plan = new ShipPlan();

        if ($id && !$ship_plan->get($id)) {
                redirect(base_url('ship_plan'));
        }
        if ($act == ACT_VALID_EDIT) {
            $ship_plan->id_wp_country = $this->input->post('country');
            $ship_plan->ship_name = $this->input->post('ship_name');
            $ship_plan->draf = $this->input->post('draf');
            $ship_plan->advertiser = $this->input->post('advertiser');
            $ship_plan->dwt = $this->input->post('dwt');
            $ship_plan->date_time = $this->input->post('date_time');
            $ship_plan->from = $this->input->post('from');
            $ship_plan->loa = $this->input->post('loa');
            $ship_plan->to = $this->input->post('to');
            $ship_plan->other_ship = $this->input->post('other_ship');
            $ship_plan->goods = $this->input->post('goods');
            $ship_plan->id_wp_purposes = $this->input->post('purposes');
            $ship_plan->id_wp_region = $this->input->post('region');
            if ($ship_plan->validateInput()) {
                if ($id) {
                    $ship_plan->modified_date = date('Y-m-d H:i:s');
                    $ship_plan->update();
                } else {
                    $ship_plan->created_date = date('Y-m-d H:i:s');
                    $ship_plan->insert();
                }
                redirect(base_url('ship_plan'));
            }
        }

        $country = Country::getList(array('disabled'=>'0'));
        $purposes = Purposes::getList();

		$array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        $region = Region::getList();
        
        $title = _LB_SHIP_PLAN;
        
        $this->data['ship_plan'] = $ship_plan;
        $this->data['title'] = $title;
        $this->data['arr_purposes'] = array(1=>_LB_GOODS_RECEIVER, 2=>_LB_GOODS_RETURN);
        $this->data['region'] = $region;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('ship_plan');
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['purposes'] = $purposes;
        $this->data['cfer'] = $cfer;
        $this->load->view('ship_plan_form', $this->data);
    }
}